package com.donn.securitycontrolplatform.positioninformation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db1.mapper.TEquipmentMapper;
import com.donn.securitycontrolplatform.db1.mapper.TPositionInfoMapper;
import com.donn.securitycontrolplatform.db2.mapper.PersonMapper;
import com.donn.securitycontrolplatform.positioninformation.entity.TPositionInfo;
import com.donn.securitycontrolplatform.positioninformation.request.PersonsTrackReq;
import com.donn.securitycontrolplatform.positioninformation.request.PositionInfoReq;
import com.donn.securitycontrolplatform.positioninformation.request.SinglePersonTrackfoReq;
import com.donn.securitycontrolplatform.positioninformation.service.ITPositionInfoService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * LOC 标签位置信息 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-06-23
 */
@Service
//@DS("db1")
public class TPositionInfoServiceImpl extends ServiceImpl<TPositionInfoMapper, TPositionInfo> implements ITPositionInfoService {

    @Autowired
    private TPositionInfoMapper positionInfoMapper;

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private TEquipmentMapper equipmentMapper;


    @Override
    public Result getLast50timeByKeyword(String keyword) {
        List<TPositionInfo> positionList;
        String userTagId = getTagIdsByUserName(keyword);
        if (StringUtil.isNotEmpty(userTagId)) {
            positionList = getPositionsByTagId(userTagId);
            return Result.success(positionList);
        }
        String equipmentTagId = getEquipmentTagIdsBy(keyword);
        if (StringUtil.isNotEmpty(equipmentTagId)) {
            positionList = getPositionsByTagId(equipmentTagId);
            return Result.success(positionList);
        }
        return Result.success(getPositionsByTagId(keyword));
    }

    @Override
    public Result getSingleTracklist(SinglePersonTrackfoReq req) {
        PositionInfoReq positionInfoReq = new PositionInfoReq();
        BeanUtils.copyProperties(req, positionInfoReq);
        if(StringUtil.isNotEmpty(positionInfoReq.getUserName())){
            String userTagId = getTagIdsByUserName(positionInfoReq.getUserName());
            positionInfoReq.setTagId(userTagId);
        }
        return Result.success(positionInfoMapper.getPositionInfo(positionInfoReq));
    }

    @Override
    public Result getPersonstTrack(PersonsTrackReq req) {
        PositionInfoReq positionInfoReq = new PositionInfoReq();
        BeanUtils.copyProperties(req, positionInfoReq);
        List<String> tagIds = getTagIdsByUserGroup(req.getUserGroup());
        positionInfoReq.setTagIds(tagIds);
        List<TPositionInfo> positionInfoList = positionInfoMapper.getPositionInfo(positionInfoReq);
        Map<String, List<TPositionInfo>> resultLMap = positionInfoList.stream().collect(Collectors.groupingBy(TPositionInfo::getTagId));
        List<List<TPositionInfo>> resultList = new ArrayList<>();
        for (Map.Entry<String, List<TPositionInfo>> entry : resultLMap.entrySet()) {
            resultList.add(entry.getValue());
        }
        return Result.success(resultList);
    }


    private List<TPositionInfo> getPositionsByTagId(String tagId) {
        PositionInfoReq positionInfoReq = new PositionInfoReq();
        positionInfoReq.setTagId(tagId);
        positionInfoReq.setLimitNum(50);
        List<TPositionInfo> positionInfoList = positionInfoMapper.getPositionInfo(positionInfoReq);
        return positionInfoList;
    }

    private String getEquipmentTagIdsBy(String keyword) {
        return equipmentMapper.selectTagIdsByNum(keyword);
    }

    @DS("db2")
    private String getTagIdsByUserName(String keyword) {
        return personMapper.selectTagIdsByUserName(keyword);
    }

    @DS("db2")
    private List<String> getTagIdsByUserGroup(String userGroup) {
        return personMapper.getTagIdsByUserGroup(userGroup);
    }
}
